<?php


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;

class AddController extends Controller
{
    public function address(Request $request)
    {
        if(session('userinfo')['id'] != null) {
        if($request->isMethod('get')){
                $sid = $request['sid'];
                $text = $request['text'];
                $res = DB::table('address')->where('addressid', session('userinfo')['id'])->get();
                $request->session()->forget('title');
                return view('index.address', ['res' => $res, 'sid' => $sid,'text' => $text]);
            }
        }else{
            echo "<script>alert('请先登录'); location.href='login_index'</script>";
        }
    }
    //删除地址
    public function address_delete(Request $request)
    {
        $id=$request->input('id');
        $row=DB::table('address')->where('id',$id)->delete();
        if($row){
            return '1';
        }
    }


    //回显修改地址
    public function address_update(Request $request)
    {
        $row = DB::table('address')->where('id', session('id'))->first();
        return view('index.address_update',['row' => $row]);

    }


    //修改地址
    public function address_alter(Request $request)
    {
        $id=$request->input('id');
        $addname=$request->input('addname');
        $dz=$request->input('dz');
        $tel=$request->input('tel');
        $say_id=$request->input('say_id');
        $row=DB::table('address')->where('id',$id)->update(['addname'=>$addname,'tel'=>$tel,'dz'=>$dz,'say_id'=>$say_id]);
        if($row!=""){
            return '1';
        }

    }


    //传id修改地址
    public function address_uid(Request $request)
    {
        $id=$request->input('id');
        if($id){
            $request->session()->put('id',$id);
            session('id',$id);
            return '1';
        }

    }

    public function address_add(Request $request)
    {
        if(session('userinfo')['id']!=null) {
            return view('index.address_add');
        }else{
            echo "<script>alert('请先登录'); location.href='login_index'</script>";
        }
    }

    public function address_site(Request $request)
    {
        // 获取要提交的数据
        $data = $request->input();
        $row = DB::table('user')->where('id', session('userinfo')['id'])->first();
        $data['addressid']=$row->addressid;
        $res=DB::table('address')->insert($data);
        if($res!=""){
            // 删除session值
            $request->session()->forget('title');
            return '1';
        }

    }

    //添加修改时把id传出去
    public function address_adds(Request $request)
    {
        //获取到传过来的title
        $title= $_GET['title'];
        if($title){
            $request->session()->put('title',$title);
            // 通过全局辅助函数
            session('title',$title);
            return '1';
        }
    }

    //添加模糊查询
    public function address_search(Request $request)
    {
        $search = $request->input('search');
        isset($search)?$search:"";
        $row=DB::table('area')->where('title', $search)->
        orWhere('title','like','%'.$search.'%')->get();
        return view('index.address_search',['row' => $row]);
    }

    //修改模糊查询
    public function address_sear(Request $request)
    {
        $search = $request->input('search');
        isset($search)?$search:"";
        $rows=DB::table('area')->where('title', $search)->
        orWhere('title','like','%'.$search.'%')->get();
        return view('index.address_sear',['rows' => $rows]);
    }

}
